/****************************************************************************
*
* Copyright (c) 2023  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : console driver interface
* HISTORY     : Initial version.
* @file     rtcapi_demo.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/

#include "rtcapi_demo.h"
#include "led_hw.h"
#include "console.h"
#include "sys.h"

#define RTCAPI_CASE1 0
#define RTCAPI_CASE2 1

static void rtccb(RTCAPIDriver *RtcApip) /* PRQA S 3673 */
{
  (void)RtcApip; /* PRQA S 0674, 3119 */
  LED_Toggle(LED1);
}

static void apic1cb(RTCAPIDriver *RtcApip)  /* PRQA S 3673 */
{
  (void)RtcApip; /* PRQA S 0674, 3119 */
  LED_Toggle(LED3);
}

/**
 * @brief   Structure defining the PWM configuration "pwmcfg".
 */
static RTCAPI_Config api_rtcapicfg =  /* PRQA S 3218 */
{
        RTCC_CLKSEL_SIRC,
        FALSE,
        64,
        rtccb,
        TRUE,
        256,
        apic1cb,
};

static RTCAPI_Config rtc_rtcapicfg =  /* PRQA S 3218 */
{
        RTCC_CLKSEL_FIRC,
        TRUE,
        1024,
        rtccb,
        FALSE,
        64,
        apic1cb,
};

/*******************************************************************************
 * @brief      RTCAPI_Test_Case1
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void RTCAPI_Test_Case1(void)
{
	if(RTC.RTCCNT.R == 0)
	{
      RTCAPI_LLD_Init(&RTCAPID1, &rtc_rtcapicfg);
      RTC_LLD_Start(&RTCAPID1);
      PSPRINTF("\n RTC will start to run ! ! ! \n");
	}
	else
      PSPRINTF("\n RTC is running in last config ! ! ! \n");
		

      PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
      delayms(200);
      PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
      delayms(200);
      PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
      delayms(200);
      PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
      delayms(200);
      PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
}

/*******************************************************************************
 * @brief      RTCAPI_Test_Case2
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void RTCAPI_Test_Case2(void) /* PRQA S 3219 */
{
	if(RTC.RTCCNT.R == 0)
	{
		RTCAPI_LLD_Init(&RTCAPID1, &api_rtcapicfg);
		RTC_LLD_Start(&RTCAPID1);
		PSPRINTF("\n RTC will start to run ! ! ! \n");
	}
	else
		PSPRINTF("\n RTC is running in last config ! ! ! \n");
	
	PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
	delayms(200);
	PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
	delayms(200);
	PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
	delayms(200);
	PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
	delayms(200);
	PSPRINTF("\n RTC counter : %x\n",RTC.RTCCNT.R);
}

/*******************************************************************************
 * @brief      RTCAPI_TEST_Demo
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void RTCAPI_TEST_Demo(void)
{
    /* Function RTC Test */
#if RTCAPI_CASE1
    RTCAPI_Test_Case1();
#endif

    /* Function API Test */
#if RTCAPI_CASE2
    RTCAPI_Test_Case2();
#endif
}
